package com.java.asviteslkyy.flashlight;

public class Batteries {

    static int count = 0;

    public Battery[] batteries;

    public Batteries() {
        this.batteries = new Battery[count];
    }

    private void grow() {
        if (count == batteries.length) {
            Battery[] tmp = new Battery[1 + count * 2];
            for (int index = 0; index < count; index++) {
                tmp[index] = this.batteries[index];
            }
            this.batteries = tmp;
        }
    }

    public void addUsedBattery(Battery battery) {
        grow();
        battery.id = count + 1;
        this.batteries[count] = battery;
        count++;
    }

    public Battery useoldBattery() {
        int index;
        do {
            Flashlight.print("Please select battery to be used:");
            index = Flashlight.getInputInt();
        } while (!Flashlight.validateInt(index, count));
        Flashlight.print("You are inserted " + batteries[index - 1].name
                + "battery with " + batteries[index - 1].voltage + " voltage");
        return batteries[index - 1];
    }

    public void showBatteries() {
        Flashlight.print("|  Id |      Name |             Voltage |");
        for (int index = 0; index < count; index++) {
            Flashlight.print(String.format("| %3s |%10s |%20s |", batteries[index].id,
                    batteries[index].name, batteries[index].voltage));
        }
    }
}
